package xyz.scootaloo.oj.offer

import org.junit.Test
import xyz.scootaloo.oj.ds.ListNode
import java.util.*

/**
 * @author flutterdash@qq.com
 * @since 2021/4/13 15:07
 */
class P24 {

    private fun reverseList(head: ListNode?): ListNode? {
        if (head == null) return null
        val vHead = ListNode(0)
        var pointer = head
        val stack = Stack<ListNode>()
        while (pointer != null) {
            stack.push(pointer)
            pointer = pointer.next
        }
        pointer = vHead
        while (stack.isNotEmpty()) {
            pointer?.next = stack.pop()
            pointer = pointer?.next
        }
        pointer?.next = null

        return vHead.next
    }

    @Test
    fun test0() {
        val node1 = ListNode(1)
        val node2 = ListNode(2)
        val node3 = ListNode(3)
        val node4 = ListNode(4)
        val node5 = ListNode(5)
        node1.next = node2
        node2.next = node3
        node3.next = node4
        node4.next = node5
        val rHead = reverseList(node1)
        println(rHead)
    }

}
